<?php
// +----------------------------------------------------------------------
// | 后台用户
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\model\User as UserModel;
use app\admin\model\Group as GroupModel;

class User extends Base {

    private $UserModel;

    public function _initialize() {
        parent::_initialize();
        $this->UserModel = new UserModel;
    }

    /**
     * 列表
     * @access public
     */
    public function index(){
        $this->isAuth('admin.user.index');
        //菜单开始
        $pathMaps = [
            ['name'=>'<i class="iconfont icon-home"></i> '.lang('lang_user_ui_title'),'url'=>'javascript:;']
        ];
        $editMenu = [
            ['name'=>'<i class="iconfont icon-cjsqm"></i> '.lang('lang_ui_add'),'url'=>url('admin/user/add')]
        ];
        $this->assign('pathMaps',json_encode($pathMaps));
        $this->assign('editMenu',json_encode($editMenu));
        //菜单结束
        $info = $this->UserModel->select_user_lists();
        $page = $info->render();
        $this->assign('page',$page);
        $this->assign('lists',$info);
        $this->assign('admin_id',$this->userinfo['admin_id']);
        return $this->fetch(); 
    }

    /**
     * 添加
     * @access public
     */

    public function add(){
        $this->isAuth('admin.user.add');
        if(request()->isAjax()){
            $data = [
                'username' => input('post.username/s'),
                'password' => input('post.password/s'),
                'group_id' => input('post.group_id/d'),
                'about'    => input('post.about/s'),
            ];
            $validate = $this->validate($data,'User.add');
            if(true !== $validate){
                return json(['code'=>1,'message'=>$validate]);
            }
            $result  = $this->UserModel->add_data($data);
            if(!$result){
                return json(['code'=>1,'message'=>lang('lang_message_error')]);
            }else{
                return json(['code'=>0,'message'=>lang('lang_message_success'),'data'=>['url'=>url('admin/user/index')]]);
            }
        }else{
            //菜单开始
            $pathMaps = [
                ['name'=>'<i class="iconfont icon-arrowleft"></i> '.lang('lang_ui_back'),'url'=>url('admin/user/index')]
            ];
            $this->assign('pathMaps',json_encode($pathMaps));
            $editMenu = [];
            $this->assign('editMenu',json_encode($editMenu));
            //菜单结束
            $group_lists = model('Group')->select_lists_user();
            $this->assign('group_lists',$group_lists);
            return $this->fetch();
        }

    }

    /**
     * 编辑用户
     * @access public
     */
    public function edit(){
        $this->isAuth('admin.user.edit');
        if(request()->isAjax()){
            $data = [
                'id' => input('post.id/s'),
                'password' => input('post.password/s'),
                'group_id' => input('post.group_id/d'),
                'about'    => input('post.about/s'),
            ];
            $validate = $this->validate($data,'User.edit');
            if(true !== $validate){
                return json(['code'=>1,'message'=>$validate]);
            }
            $result  = $this->UserModel->edit_data($data);
            if(!$result){
                return json(['code'=>1,'message'=>lang('lang_message_error')]);
            }else{
                return json(['code'=>0,'message'=>lang('lang_message_success'),'data'=>['url'=>url('admin/user/index')]]);
            }
        }else{
            $id = input('id/d');
            //菜单开始
            $pathMaps = [
                ['name'=>'<i class="iconfont icon-arrowleft"></i> '.lang('lang_ui_back'),'url'=>url('admin/user/index')]
            ];
            $this->assign('pathMaps',json_encode($pathMaps));
            $editMenu = [];
            $this->assign('editMenu',json_encode($editMenu));
            if($id == $this->userinfo['admin_id']){
                $this->success(lang('lang_user_edit_error'));
            }
            //菜单结束
            $group_lists = model('Group')->select_lists_user();
            $result = $this->UserModel->find_user_info($id);
            $this->assign('group_lists',$group_lists);
            $this->assign('info',$result);
            return $this->fetch(); 
        }
    }

    /**
     * [删除]
     * @access public
     * @return bool
     */
    public function delete(){
        $this->isAuth('admin.user.delete');
        $id   = input('id/d');
        if($id == $this->userinfo['admin_id']){
            return json(['code'=>1,'message'=>lang('lang_user_delete_error')]);
        }
        $result  = $this->UserModel->delete_data($id);
        if(!$result){
            return json(['code'=>1,'message'=>lang('lang_message_error')]);
        }else{
            return json(['code'=>0,'message'=>lang('lang_message_success')]);
        }

    }

    /**
     * 修改密码
     * @access public
     */
    public function password(){
        if(request()->isAjax()){
            $data = [
                'password' => input('post.password/s'),
                'about'    => input('post.about/s'),
            ];
            $validate = $this->validate($data,'User.password');
            if(true !== $validate){
                return json(['code'=>1,'message'=>$validate]);
            }
            $result  = $this->UserModel->upDatePasspowrd($data,$this->userinfo['admin_id']);
            if(!$result){
                return json(['code'=>1,'message'=>lang('lang_update_password_error')]);
            }else{
                return json(['code'=>0,'message'=>lang('lang_update_password_success'),'data'=>['url'=>url('admin/index/logout')]]);
            }
        }else{
            $pathMaps = [
                ['name'=>'<i class="iconfont icon-jiaoseguanli-"></i> '.lang('lang_ui_password'),'url'=>'javascript:;']
            ];
            $this->assign('pathMaps',json_encode($pathMaps));
            $result = $this->UserModel->find_user_info($this->userinfo['admin_id']);
            $this->assign('info',$result);
            return $this->fetch(); 
        }
    } 
}